Transform Weather Sensor Data from Waggle to an Xarray Dataset + Plot with ACT#
Imports#
import sage_data_client
from bokeh.models.formatters import DatetimeTickFormatter
import hvplot.pandas
import hvplot.xarray
import holoviews as hv
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import xarray as xr
import matplotlib.pyplot as plt
from metpy.plots import USCOUNTIES
import act
import numpy as np
import pandas as pd
import warnings
from bokeh.models import DatetimeTickFormatter
def apply_formatter(plot, element):
plot.handles['xaxis'].formatter = DatetimeTickFormatter(hours='%m/%d/%Y \n %H:%M',
minutes='%m/%d/%Y \n %H:%M',
hourmin='%m/%d/%Y \n %H:%M',
days='%m/%d/%Y \n %H:%M',
months='%m/%d/%Y \n %H:%M')
xr.set_options(keep_attrs=True)
warnings.filterwarnings("ignore")
hv.extension("bokeh")
Query for the available Data#
wxt_df = sage_data_client.query(
start="-3h",
filter={
"sensor": "vaisala-wxt536"
}
)
Configure Helper Functions and Renaming Conventions#
The renaming is required due to . notations being problematic when working with both Pandas and Xarray data structures.
variable_rename_dict = {'wxt.env.humidity':'air_humidity',
'wxt.env.pressure':'air_pressure',
'wxt.env.temp':'air_temperature',
'wxt.heater.temp':'heater_temperature',
'wxt.heater.volt':'heater_voltage',
'wxt.rain.accumulation':'rain_accumulation',
'wxt.wind.direction':'wind_direction',
'wxt.wind.speed':'wind_speed',
'sys.gps.lat':'latitude',
'sys.gps.lon':'longitude',
}
def generate_data_array(df, variable, rename_variable_dict=variable_rename_dict):
new_variable_name = rename_variable_dict[variable]
df_variable= df.loc[df.name == variable]
ds = df_variable.to_xarray().rename({'value':new_variable_name,
'timestamp':'time',
'meta.vsn':'node'})
ds[new_variable_name].attrs['units'] = df_variable['meta.units'].values[0]
ds['time'] = pd.to_datetime(ds.time)
ds.attrs['datastream'] = ds.node.values[0]
return ds[[new_variable_name]]
def generate_dataset(df, variables, rename_variable_dict=variable_rename_dict):
reindexed = df.set_index(['meta.vsn', 'timestamp'])
return xr.merge([generate_data_array(reindexed, variable) for variable in variables])
Transform the Data to Xarray#
wxt_variables = wxt_df.name.unique()
wxt_variables
array(['wxt.env.humidity', 'wxt.env.pressure', 'wxt.env.temp',
'wxt.heater.temp', 'wxt.heater.volt', 'wxt.rain.accumulation',
'wxt.wind.direction', 'wxt.wind.speed'], dtype=object)
wxt_ds = generate_dataset(wxt_df, wxt_variables).squeeze()
wxt_ds
<xarray.Dataset>
Dimensions: (node: 2, time: 151009)
Coordinates:
* node (node) object 'W01B' 'W067'
* time (time) datetime64[ns] 2023-10-31T11:21:54.617147746 ....
Data variables:
air_humidity (node, time) float64 85.3 85.3 85.3 ... nan 100.0 nan
air_pressure (node, time) float64 1.005e+03 1.005e+03 ... 975.8 nan
air_temperature (node, time) float64 -2.7 -2.7 -2.7 ... nan -1.2 nan
heater_temperature (node, time) float64 2.1 2.1 2.1 2.1 ... nan 14.7 nan
heater_voltage (node, time) float64 11.3 11.3 11.3 ... nan 10.7 nan
rain_accumulation (node, time) float64 64.86 64.86 64.86 ... nan 25.98 nan
wind_direction (node, time) float64 130.0 130.0 130.0 ... nan 53.0 nan
wind_speed (node, time) float64 1.1 1.1 1.1 1.1 ... 0.2 nan 0.2 nan
Attributes:
datastream: W01BResample the data to minute requency#
minute_ds = wxt_ds.resample(time='1T').mean()
Visualize using hvplot#
meteogram_variables = ['air_temperature', 'air_humidity', 'wind_speed', 'wind_direction']
plots = []
for variable in meteogram_variables:
plots.append(wxt_ds[variable].hvplot.line(label='10 Hz Data') *
minute_ds[variable].hvplot.line(label='1 Minute Data'))
hv.Layout(plots).cols(2)